package com.example.newfragment;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import com.bitmap.mysqlite.BitmapHelper;
import com.boyuan.huijiaoyu.R;
import com.magicsoft.app.adapter.CTHAdapter;
import com.magicsoft.app.helper.SharePreferenceHelper;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class HomeContactAdapter1 extends CTHAdapter<DetailPerson>{
	
	private DisplayImageOptions options;
	private ImageLoader imageLoader;
	private Context context;
	

	public HomeContactAdapter1(Context context, List<DetailPerson> data, DisplayImageOptions options, ImageLoader imageLoader) {
		super(context, data);
		this.options = options;
		this.imageLoader = imageLoader;
		this.context = context;
	}
	
	@Override
	public int getCount() {
		return super.getCount();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		HolderViewStatic viewContent = null;
		DetailPerson contactEntity = data.get(position);
		if(convertView == null){
			viewContent = new HolderViewStatic();
			convertView = mInflater.inflate(R.layout.home_contact_common_item, null);
			viewContent.imageView = (ImageView) convertView.findViewById(R.id.imageview101);
			viewContent.tv_job = (TextView) convertView.findViewById(R.id.tv_job);
			viewContent.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			viewContent.iv_favorite = (ImageView) convertView.findViewById(R.id.iv_favorite);
			convertView.setTag(viewContent);
		}else{
			viewContent = (HolderViewStatic) convertView.getTag();
		}
		
		if(contactEntity != null){
			String mobile = contactEntity.getMobile();
			String realname = contactEntity.getRealname();
			String job_name = contactEntity.getFamily_name();
			String is_favorite = contactEntity.getIs_favorite();
			viewContent.tv_job.setText(job_name);
			viewContent.tv_name.setText(realname);
			if("1".equals(is_favorite)){
				viewContent.iv_favorite.setSelected(true);
				viewContent.iv_favorite.setVisibility(View.VISIBLE);
			}else if("2".equals(is_favorite)){
				viewContent.iv_favorite.setSelected(false);
				viewContent.iv_favorite.setVisibility(View.VISIBLE);
			}
			options = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.drawable.default_header)
			.showImageForEmptyUri(R.drawable.default_header)
			.showImageOnFail(R.drawable.default_header).cacheInMemory(true)
			.cacheOnDisc(true).considerExifParams(true).cacheOnDisk(true)
			.build();
//			String uri = Constant.photourl + contactEntity.getUid();
			String uri = contactEntity.getAvatar();
			imageLoader.displayImage(uri,viewContent.imageView);
//			Bitmap bitmap = BitmapHelper.readbitmap(context, mobile);
//			Log.e("bitmap", ""+position);
//			if(bitmap != null){
//				viewContent.imageView.setImageBitmap(bitmap);
//			}else{
//				ImageAsynTask imageAsynTask = new ImageAsynTask(viewContent.imageView, mobile);
//				imageAsynTask.execute(uri);
//			}
		}
		return convertView;
	}
	
	static class HolderViewStatic {
		ImageView imageView;
		TextView tv_name;
		TextView tv_job;
		ImageView iv_favorite;
	}
	
	class ImageAsynTask extends AsyncTask<String, Void, Bitmap>{

		ImageView image;
		String str;
		
		public ImageAsynTask(ImageView image, String url){
			this.image = image;
			this.str = url;
		}
		
		@Override
		protected Bitmap doInBackground(String... params) {
			Bitmap bitmap = null;
			String path = params[0];
			try {
				URL url = new URL(path);
				URLConnection openConnection = url.openConnection();
				InputStream inputStream = openConnection.getInputStream();
				Options options2 = new BitmapFactory.Options();
				options2.inSampleSize = 4;
				bitmap = BitmapFactory.decodeStream(inputStream, null, options2);
				BitmapHelper.savebitmap(context, str, bitmap);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return bitmap;
		}
		
		@Override
		protected void onPostExecute(Bitmap result) {
			if(image != null){
				image.setImageBitmap(result);
			}
			super.onPostExecute(result);
		}
	}
	
}
